package com.bawei.yuekao;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.Manifest;
import android.os.Build;
import android.os.Bundle;

import com.bawei.yuekao.adapter.MyFragmentPagerAdapter;
import com.bawei.yuekao.fragment.HomeFragment;
import com.bawei.yuekao.fragment.LikeFragment;
import com.bawei.yuekao.fragment.MeFragment;
import com.bawei.yuekao.fragment.MusicFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 1。权限 依赖 图片资源
 * 2。主页面搭建：底部4个fragment， 其中嵌套2个fragment
 *
 */
public class MainActivity extends AppCompatActivity {
    private ViewPager viewPager;
    private TabLayout tab;
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;
    private List<Fragment> mList = new ArrayList<>();
    private List<String> titles = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initPermissions();
        initView();
        initData();
    }

    private void initData() {
        //准备数据源
        titles.add("首页");
        titles.add("音乐");
        titles.add("收藏");
        titles.add("我的");
        mList.add(new HomeFragment());
        mList.add(new MusicFragment());
        mList.add(new LikeFragment());
        mList.add(new MeFragment());
        //适配器
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), FragmentPagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mList,titles);
        //viewpager设置适配器
        viewPager.setAdapter(mMyFragmentPagerAdapter);
        //viewpager和tablayout上下联动
        tab.setupWithViewPager(viewPager);

    }

    private void initView() {
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        tab = (TabLayout) findViewById(R.id.tab);
    }

    //动态获取权限
    private void initPermissions() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.READ_CONTACTS,
                    Manifest.permission.WRITE_CONTACTS,
                    Manifest.permission.READ_SMS,
                    Manifest.permission.CALL_PHONE,
                    Manifest.permission.ACCESS_NETWORK_STATE,
                    Manifest.permission.READ_PHONE_STATE,
                    Manifest.permission.WRITE_CALL_LOG,
                    Manifest.permission.READ_CALL_LOG
            },101);
        }
    }
}